package com.leyou.item.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.page.pageResult;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.TbBrand;
import com.leyou.item.service.TbBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("brand")
public class BrandController {

    @Autowired
    private TbBrandService brandService;

    /*
        接口路径
        GET /brand/page?key=&page=1&rows=5&sortBy=id&desc=false
        参数：key  搜索的关键字 可以不传
              page; 当前页数 默认值1
              rows: 每页显示的记录数 默认值5
              sortBy:根据排序的字段 默认值id 可以不传
              desc：选择排序的方式 可以不传，默认值false
        响应数据：pageResult
               进行了分页功能


    */
    @GetMapping("/page")
    public ResponseEntity<pageResult<BrandDTO>> findBrand(@RequestParam(value = "key",required = false) String key,
                                              @RequestParam(value = "page",defaultValue = "1")Integer page,
                                              @RequestParam(value = "rows",defaultValue = "5")Integer rows,
                                              @RequestParam(value = "sortBy",required = false)String sortBy,
                                              @RequestParam(value = "desc",defaultValue ="false")boolean desc
                                              ){

        pageResult<BrandDTO> pageResult=brandService.findBrand(key,page,rows,sortBy,desc);

        return ResponseEntity.status(HttpStatus.OK).body(pageResult);
    }


    @PostMapping
    public ResponseEntity insertBrand(BrandDTO brandDTO,@RequestParam("cids")List<Long> cids){

        brandService.saveBrand(brandDTO,cids);

        return ResponseEntity.status(HttpStatus. CREATED).build() ;
    }

    /*修改数据：from表单提交：DTO对象接收数据*/
    @PutMapping
    public ResponseEntity updateBrand(BrandDTO brandDTO,@RequestParam("cids")List<Long> cids){

      brandService.updateBrand(brandDTO,cids);

      return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    /*根据分类id查询，品牌数据*/
  @GetMapping("of/category")
    public ResponseEntity<List<BrandDTO>> findBrandByCategoryId(@RequestParam("id")Long id){

      List<BrandDTO>  brandDTOList= brandService.findBrandByCategoryId(id);

      return  ResponseEntity.status(HttpStatus.OK).body(brandDTOList);

  }
  @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> findBrandById(@PathVariable("id")Long id){
      TbBrand tbBrand = brandService.getById(id);
      /*对象转化*/
      if(tbBrand==null){
          throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
      }
      BrandDTO brandDTO = BeanHelper.copyProperties(tbBrand, BrandDTO.class);
      return ResponseEntity.status(HttpStatus.OK).body(brandDTO);
  }

  @GetMapping("list")
    public ResponseEntity<List<BrandDTO>> findBrandByIds(@RequestParam("ids")List<Long> ids){

      Collection<TbBrand> tbBrands = brandService.listByIds(ids);

      if(CollectionUtils.isEmpty(tbBrands)){
          throw  new LyException(ExceptionEnum.BRAND_NOT_FOUND);
      }
      List<BrandDTO> brandDTOList = tbBrands.stream().map(tbBrand -> {
          BrandDTO brandDTO = BeanHelper.copyProperties(tbBrand, BrandDTO.class);
          return brandDTO;
      }).collect(Collectors.toList());

      return ResponseEntity.status(HttpStatus.OK).body(brandDTOList);
  }
}
